<?php
namespace Lyricist\Controller;
use Common\Controller\LyricistController;

class IndexController extends LyricistController {


    public function __construct()
    {
        $sessionid = 'PHPSESSID='.$this->getSessionId();

        $getsessionid = $_SERVER['HTTP_COOKIE'];
        if ($sessionid!=$getsessionid){
            $this->returnData('','状态已过期请重新授权',400);
        }
    }

    /**
     *  获取基本信息
     */
    public function getinfo(){

        $appid = I('post.appid','','htmlspecialchars');
        //规则
        $data['rule'] = array('完成挑战即可获得随机红包','累计金额达到5元可以发起提现。','提现请联系客服，工作人员将定时为您发放红包奖励。','放假期间回复可能有延误，我们工作人员看到消息会第一时间为您发放红包，请见谅！');

        $data['Result'] = array(
            'ShareThumb'=>$this->url.'lyricist/image/share2.jpg',
            'ShareTitle'=>'识别错别字,赢红包,敢来一局吗？',
            'Music'=>array(
                'Ready'=>$this->url.'lyricist/music/ready2.mp3',
                'Success'=>$this->url.'lyricist/music/success.mp3',
                'Fail'=>$this->url.'lyricist/music/fail.mp3',
                'Quick'=>$this->url.'lyricist/music/quick.mp3',
                'Slow'=>$this->url.'lyricist/music/slow.mp3',
                'Dian'=>$this->url.'lyricist/music/dian2.mp3',
                'Beijin'=>$this->url.'lyricist/music/beijin1.mp3',
                'Error'=>$this->url.'lyricist/music/error.mp3',
                'Countdown'=>$this->url.'lyricist/music/countdown.mp3',
                'Readygo'=>$this->url.'lyricist/music/readygo.mp3',
                'Readygo1'=>$this->url.'lyricist/music/readygo2.mp3',
            ),

        );

        $data['onoff'] = 0; //搜码进来是否跳出网络错误

    /*    $data['float'] = array(
            'appid'=>'wx6b1c7907c5733fc9',
            'url'=>'/pages/index/index?appid=wx370386b7eb0c04bc&pages=pages/index/index',
            'imgsrc'=>'http://oss.beikg.cn/appshow/newsonglogo.jpg',
        );
        */

        $data['FhPopup'] = array( //分享复活
            'Title'=>'',
            'Content'=>array('分享到群聊，即可继续答题','今日分享过的群，不会复活哟'),
            'Button'=>'立即复活',
        );
      
        $sharetitle = M('lyricist_config')->where(array('category'=>'lyricist'))->getField('sharetitle');
        if (empty($sharetitle)){
            $sharetitle = '邀请群友领红包';
        }
      
        $data['SharePopup'] = array(
            'Title'=>$sharetitle,//获得更多能量
            'Content'=>array(
                '分享到群聊，即可获得能量值','今日分享过的群，不会获得能量值哟~'
            ),
            'Button'=>'邀请好友一起来挑战'
        );

        $data['JiPopup'] = array(
            'Title'=>'',
            'Content'=>array(
                '您还没有领取机会','赶快去挑战领取机会吧'
            ),
            'Button'=>'确定'
        );

        $data['IndexButton'] = '推荐好友一起来玩';

        $data['moreplay'] = array(
            'url'=>'pages/index/index',
            'appid'=>'wx043045a1599b9737',
        ); //更多好玩
	/*
        $data['indexappbutton'] = array(
            array(
                'appid'=>'wx6b1c7907c5733fc9',
                'title'=>'猜歌名赢奖金 ',
                'url'=>'/pages/index/index?appid=wx47f3a2edad028bdf&pages=pages/index/index',
                'imgsrc'=>$this->url.'lyricist/image/music.png',
            ),
            array(
                'appid'=>'wx6b1c7907c5733fc9',
                'title'=>'算加减赢奖金 ',
                'url'=>'/pages/index/index?appid=wxc585cb6b9c965bcf&pages=pages/index/index',
                'imgsrc'=>$this->url.'lyricist/image/susuan.png',
            ),
            array(
                'appid'=>'wx6b1c7907c5733fc9',
                'title'=>'玩游戏赢好礼 ',
                'url'=>'/pages/index/index?appid=wxbcb264959c7f8ffe&pages=pages/index/index',
                'imgsrc'=>$this->url.'lyricist/image/hezi.png',
            ),
        );
        

        $data['advertising'] = $this->advertising(); //广告
*/

        $data['tousu'] = array(
            array(
                'title'=>'色情',
                'id'=>'1',
            ),
            array(
                'title'=>'骚扰',
                'id'=>'2',
            ),
            array(
                'title'=>'欺诈',
                'id'=>'3',
            ),
            array(
                'title'=>'传播谣言',
                'id'=>'4',
            ),
            array(
                'title'=>'内容不符',
                'id'=>'5',
            ),
            array(
                'title'=>'恶意收集信息',
                'id'=>'6',
            ),
            array(
                'title'=>'侵权',
                'id'=>'7',
            ),
            array(
                'title'=>'诱导',
                'id'=>'8',
            ),

        );



        $this->returnData($data);

    }

    /**
     * 截屏
     */
    public function getscreen(){

        $appid = I('post.appid','','htmlspecialchars');
        $uid = I('post.uid','','int');


        if (!$appid||!$uid){
            $this->returnData('','非法操作',400);
        }
        $data['screen'] = 1;
        $data['screen_time'] = time();

        M('lyricist_member')->where(array('uid'=>$uid))->save($data);

        $data['status'] = 1;
        $this->returnData($data);
    }

    //投诉
    public function tousu(){

        $data = array();
        $data['uid']= I('post.uid','','htmlspecialchars');

        $data['appid'] = I('post.appid','','htmlspecialchars');

        $data['tid'] = I('post.id','','int');

        $data['title'] = I('post.title','','htmlspecialchars');

        $data['create_time'] = date('Y-m-d H:i:s',time());

        M('lyricist_tousu')->add($data);

        $this->returnData();
    }


    /**
     * 广告
     */
    private function advertising(){

        $data = M('lyricist_ad')->where(array('isshow'=>'1'))->select();

        foreach ($data as $key=>$val){

            $data[$key]['logo'] = $this->url.$data[$key]['logo'];

        }


        return $data;

    }


    //提现记录
    public function cash(){

//        $field                          =   array('nickname','avatarurl','money');
//        $where_cash_log['cash_status']  =   array('EGT','0');
//        $result   =   M('lyricist_cash')->where($where_cash_log)->field( $field )->select();
//
//        foreach ($result as $key=>$val){
//
//            $result[$key]['title'] = '成功提现'.$result[$key]['money'].'元';
//
//        }
        $appid        =   I('post.appid','','htmlspecialchars');
        $result = $this->cachecash($appid);

        $this->returnData($result);

    }

    //获取机会
    public function prize_num()
    {
        $uid        =   I('post.uid','','htmlspecialchars');

        $appid        =   I('post.appid','','htmlspecialchars');

        if (! empty($uid))
        {
            $where          =   array();
            $data           =   array();
            $where['uid']   =   $uid;
            $field          =   array('uid','openid','game_num','totle_num','prize_num','money_total','play_time','play_num','total_money','scene');
            $member_data    =   M('lyricist_member')->where($where)->field($field)->find();

            if (! empty($member_data))
            {
                $today_time     =   strtotime(date('Y-m-d',time()));
                //限制用户一天玩的次数

                $config = M('lyricist_config')->field('play_num,isfree')->where(array('category'=>'lyricist'))->find(); //配置文件
                $play_num = $config['play_num'];
                if (empty($play_num)){
                    $play_num = 10;
                }

                $isfree = 0;
                if ($member_data['money_total']>2.75){
                    $play_num = $this->updatetag($uid,$member_data['money_total'],$play_num);
                    if ($play_num==2){ //羊毛党
                        $isfree = 0;
                    }
                }
           

                if ($member_data['play_time'] > $today_time && $member_data['play_num'] >= $play_num)
                {
                    $data['game_num']   =   $member_data['game_num'];
                    $data['totle_num']  =   $member_data['totle_num'];
                    $data['money_total']=   $member_data['money_total'];
                    $data['prize_num']  =   $member_data['prize_num'];
                    $this->returnData($data,$msg='当天挑战次数达上限,明天再来',$code=400);
                }

                if ($member_data['play_time'] > $today_time && $member_data['game_num'] == 0&&$member_data['scene']=='1011'&&$member_data['total_money'] >=1)  //搜码进来的
                {
                    $data['game_num']   =   $member_data['game_num'];
                    $data['totle_num']  =   $member_data['totle_num'];
                    $data['money_total']=   $member_data['money_total'];
                    $data['prize_num']  =   $member_data['prize_num'];
                    $this->returnData($data,$msg='当天挑战次数达上限,明天再来',$code=400);
                }

                $data['game_num']   =   $member_data['game_num'];

                if(empty($member_data['game_num']) && $member_data['play_time'] < $today_time)
                {
                    $data['game_num']   =   1;
                }

            
                if (empty($member_data['game_num'])||$member_data['game_num']==0) {
                    if ($member_data['total_money'] <1&&$member_data['play_num']<4) {  //金额小于1 随便玩
                        $data['game_num'] = 1;
                    }elseif ($isfree==1){  //开启免费模式
                        $data['game_num'] = 1;
                    }
                }

                $data['totle_num']  =   $member_data['totle_num'];
                $data['money_total']=   $member_data['money_total'];
                $data['prize_num']  =   $member_data['prize_num'];
                $this->returnData($data,$msg='OK',$code=200);
            }
        }

        $this->returnData($data,$msg='用户id参数不能为空',$code=404);
    }


    //获取题目
    public function new_problem()
    {
        $uid        =   I('post.uid','','htmlspecialchars');
        $share_uid  =   I('post.share_uid','','htmlspecialchars');

        $appid = I('post.appid','','htmlspecialchars');


        if (! empty($uid))
        {
            $where          =   array();
            $data           =   array();
            $where['uid']   =   $uid;
            $field          =   array('uid','openid','session_key','game_num','totle_num','play_time','play_num','total_money','money_total');
            $member_data    =   M('lyricist_member')->where($where)->field($field)->find();

            if (! empty($member_data))
            {
                $time = time();
                //绑定好友
                if (! empty( $share_uid) &&  $share_uid != $uid)
                {
                    $this->share_fraend($share_uid,$uid,$appid);
                }

                $today_time     =   strtotime(date('Y-m-d'));

                $config = M('lyricist_config')->field('play_num,isfree')->where(array('category'=>'lyricist'))->find(); //配置文件
                $play_num = $config['play_num'];

                if (empty($play_num)){
                    $play_num = 10;
                }
                if ($member_data['money_total']>2.75){
                    $play_num = $this->updatetag($uid,$member_data['money_total'],$play_num);
                    if ($play_num==2){ //羊毛党
                        $isfree = 0;
                    }
                }
                //限制用户一天玩的次数
                if ($member_data['play_time'] > $today_time && $member_data['play_num'] >= $play_num)
                {
                    $this->returnData($data,$msg='当天挑战次数达上限,明天再来',$code=400);
                }


                if ($member_data['game_num'] > 0 || ($member_data['play_time'] < $today_time)|| $member_data['total_money'] <1 ||$isfree==1)
                {
                    //减去一次机会
                    $update_data                =   array();
                    if ($member_data['play_time'] >= $today_time&&$member_data['game_num']>0)
                    {
                        $update_data['game_num']    =   $member_data['game_num']    -   1;
                    }
                    $update_data['totle_num']   =   $member_data['totle_num']   +   1;
                    $update_data['update_time'] =   $time;

                    if($member_data['play_time'] > $today_time)
                    {
                        $update_data['play_time']   =   $time;
                        $update_data['play_num']    =   $member_data['play_num']    +   1;
                    }
                    else
                    {
                        $update_data['play_time']   =   $time;
                        $update_data['play_num']    =   1;
                    }

                    if ($member_data['total_money']==0){
                        $update_data['play_time']   =   $time;
                       // $update_data['play_num']    =   1;
                    }


                    M('lyricist_member')->where($where)->save($update_data);


                    $data['success_number']     =   5;
                    $data['question']           =   $this->getquestion($uid,$time,$appid);


                    $this->returnData($data,$msg='ok',$code=200);
                }
                else
                {
                    $this->returnData($data,$msg='你已经用完机会了',$code=400);
                }
            }
        }

        $this->returnData($data,$msg='用户id参数不能为空',$code=404);
    }




    /**
     * 分享好友
     * @param $share_uid
     * @param $uid
     */
    private function share_fraend($share_uid,$uid,$appid)
    {
        $where          =   array();
        $where['uid']   =   $share_uid;
        $field          =   array('uid');
        $member_data    =   M('lyricist_member')->where($where)->field($field)->find();
        if (! empty($member_data))
        {
            $where_relationship                         =   array();
            $where_relationship['my_member_id']         =   $share_uid;
            $where_relationship['coupling_member_id']   =   $uid;
            $field_relationship                         =   array('id');
            $result     =   M('lyricist_relationship')->where($where_relationship)->field($field_relationship)->find();

            if (empty($result))
            {
                $insert_data                        =   array();
                $insert_data['my_member_id']        =   $share_uid;
                $insert_data['coupling_member_id']  =   $uid;
                $insert_data['createtime']          =   time();
                $insert_data['appid']               =  $appid;
                M('lyricist_relationship')->add($insert_data);
            }
        }
    }

    //回答
    public function new_answer()
    {

        $member_id = I('post.uid','','int');

        $answer = $_POST['answer'];

        $appid = I('post.appid','','htmlspecialchars');
        if (!$member_id||!$answer){
            $this->returnData('','非法操作',401);
        }

        $filed = array('appid','play_time','money_total','total_money','prize_num');

        $memberdata = M('lyricist_member')->field($filed)->where(array('uid'=>$member_id))->find();

        if (empty($memberdata)){
            $this->returnData('','用户不存在',100);
        }
        $where['uid'] = $member_id;
        $where['create_time'] = $memberdata['play_time'];
        $where['appid'] = $memberdata['appid'];
        $where['state'] = 0;

        $baoanswer = M('lyricist_questions_played')->field('anwser,id')->where($where)->find();

        $wherecount['total_money'] = array('gt',0);
        $wherecount['appid'] = $appid;
        $count = M('lyricist_member')->where($wherecount)->count();

        $myanswer = json_decode($baoanswer['anwser']);

        $answer = json_decode($answer);
        $data['count'] = $count+1234;

        if ($answer!=$myanswer){ //验证题目是否全对
            $data['challenge_id']   =   0;
            $this->returnData($data,'答错了',100);
        }

        //更新通关数
        $update_data                =   array();
        $update_data['prize_num']   =   $memberdata['prize_num']   +   1;
        M('lyricist_member')->where(array('uid'=>$member_id))->save($update_data);

        M('lyricist_questions_played')->where($where)->save(array('state'=>'1')); //更新题目状态

        //获取红包
        $data['challenge_id']   =   $this->create_redbag($member_id,$memberdata['appid'],$baoanswer['id']);

        $data['msg']            =   '恭喜你通过所有关卡';

        $this->returnData($data,$msg='恭喜你通过所有关卡',$code=200);

    }

    /**
     * 排行榜
     */
    public function rank()
    {
        $appid = I('post.appid','','htmlspecialchars');
        $uid = I('post.uid','','int');
        $state = I('post.state','1','int');


        $filed = array('uid','money_total','total_money','prize_num','totle_num','nickname','avatarurl');

        $data = array();

        $member_data = M('lyricist_member')->field($filed)->where(array('uid'=>$uid))->find();


            $member_data['ranking'] = '未上榜';
            if ($state==1) { //荣誉排行

                $data['rank'] = $this->cacherank($appid);
                if (!empty($member_data['uid'])){
                    foreach ($data['rank'] as $key => $val) {
                        $data['rank'][$key]['ranking'] = $key + 1;
                        if ($val['uid'] == $member_data['uid']) {
                            $member_data['ranking'] = $key + 1;
                        }
                    }

                $data['myrank'] = $member_data;

                }
            }

            if ($state==2){ //毅力排行

                $filed2 = array('totle_num','nickname','avatarurl','uid');
                $where['appid'] = $appid;
                $data['rank'] = M('lyricist_member')->field($filed2)->where($where)->order('totle_num desc')->limit(50)->select();
                if (!empty($member_data['uid'])) {
                    foreach ($data['rank'] as $key => $val) {
                        $data['rank'][$key]['ranking'] = $key + 1;
                        if ($val['uid'] == $uid) {
                            $member_data['ranking'] = $key + 1;
                        }
                    }

                    $data['myrank'] = $member_data;
                }
            }

        $this->returnData($data,'ok',200);

    }


    //分享群
    public function get_numbers()
    {

        $data       =   array();
        $uid        =   I('post.uid','','htmlspecialchars');
        $endata     =   I('post.endata','','htmlspecialchars');
        $vi         =   I('post.vi','','htmlspecialchars');


        if (! empty($endata) && ! empty($uid) && ! empty($vi))
        {
            $where          =   array();
            $where['uid']   =   $uid;

            $field          =   array('uid','openid','session_key','game_num','play_time','appid');
            $member_data    =   M('lyricist_member')->where($where)->field($field)->find();

            if (! empty($member_data))
            {

                //解析openGId
                $openGId = $this->getopenGId($member_data['appid'], $member_data['session_key'], $endata, $vi);


                if ($openGId != '-1')
                {
                    //创建群
                    $where_group                =   array();
                    $where_group['uid']         =   $uid;
                    $where_group['opengid']     =   $openGId;
                    $where_group['sharetime']   =   date('Y-m-d',time());
                    $field                      =   array('id','opengid','sharenum','sharetime');
                    $group_data                 =   M('lyricist_wechat_group')->where($where_group)->field($field)->find();
                    if (! empty($group_data))
                    {
                        $update_data                =   array();
                        $update_data['sharetime']   =   date('Y-m-d',time());
                        $update_data['sharenum']    =   $group_data['sharenum'] +   1;
                        M('lyricist_wechat_group')->where($where_group)->save($update_data);

                        $sharetwo = M('lyricist_config')->where(array('category'=>'lyricist'))->getField('sharetwo');
                        if (!empty($sharetwo)&&$sharetwo==1&&$group_data['sharenum']<2){ //二次分享有效
                            //加机会
                            $update_member                  =   array();
                            $update_member['update_time']   =   time();
                            $update_member['game_num']      =   $member_data['game_num']    +   1;
                            $result     =   M('lyricist_member')->where($where)->save($update_member);

                            if (! empty($result))
                            {
                                $msg            =   '分享成功';
                                $data['msg']    =   '分享成功';

                                $this->returnData($data,$msg,$code=200);
                            }
                        }

                        $msg='本群已分享过';

                        $this->returnData($data,$msg,$code=400);
                    }
                    else
                    {
                        $insert_data                =   array();
                        $insert_data['opengid']     =   $openGId;
                        $insert_data['uid']         =   $uid;
                        $insert_data['sharetime']   =   date('Y-m-d',time());
                        $insert_data['sharenum']    =   1;
                        $insert_data['create_time'] =   time();
                        $insert_data['appid'] =   $member_data['appid'];
                        $result     =   M('lyricist_wechat_group')->add($insert_data);

                        if (! empty($result))
                        {
                            //加机会
                            $update_member                  =   array();
                            $update_member['update_time']   =   time();
                            $update_member['game_num']      =   $member_data['game_num']    +   1;
                            $result     =   M('lyricist_member')->where($where)->save($update_member);

                            if (! empty($result))
                            {
                                $msg            =   '分享成功';
                                $data['msg']    =   '分享成功';

                                $this->returnData($data,$msg,$code=200);
                            }
                        }
                    }
                }
            }
        }

        $this->returnData($data,$msg='已分享',$code=400);
    }

    /**
     * 获取红包
     */
    public function award_total()
    {
        $uid                =   I('post.uid','','htmlspecialchars');
        $challenge_id       =   I('post.challenge_id','','htmlspecialchars');
        $appid = I('post.appid','','htmlspecialchars');

        $data                   =   array();
        if (! empty($challenge_id) && ! empty($uid))
        {

            $where                  =   array();
            $field                  =   array('red_total','status','total_sn','create_time','red_total');
            $where['member_id']     =   $uid;
            $where['challenge_id']  =   $challenge_id;
            $newsong_redbag         =   M('lyricist_redbag')->where($where)->field($field)->find();
            if (! empty($newsong_redbag))
            {
                //更新数据
                if (empty($newsong_redbag['status']))
                {
                    $where_member           =   array();
                    $field                  =   array('money_total','total_money');
                    $where_member['uid']    =   $uid;
                    $member_data            =   M('lyricist_member')->where($where_member)->field($field)->find();

                    if (! empty($member_data))
                    {
                        //开启事务更新数据
                        $Model = M();
                        $Model->startTrans();

                        //更新红包
                        $update_redbag                  =   array();
                        $update_redbag['status']        =   1;
                        $update_redbag['update_time']   =   date('Y-m-d H:i:s',time());

                        $result     =  $Model->table('mj_lyricist_redbag')->where($where)->save($update_redbag);
                        if (! empty($result))
                        {
                            //更新红包记录
                            $update_log                  =   array();
                            $update_log['status']        =   1;
                            $update_log['update_time']   =   date('Y-m-d H:i:s',time());
                            $result     =   $Model->table('mj_lyricist_redbag_log')->where($where)->save($update_log);

                            if (! empty($result))
                            {
                                //更新用户数据
                                $update_member                  =   array();
                                $update_member['money_total']   =   $member_data['money_total'] + $newsong_redbag['red_total'];
                                $update_member['total_money']   =   $member_data['total_money'] + $newsong_redbag['red_total'];
                                $update_member['update_time']   =   time();
                                $result     =   $Model->table('mj_lyricist_member')->where($where_member)->save($update_member);

                                if (! empty($result))
                                {
                                    //更新收支记录
                                    $where_change_log                   =   array();
                                    $where_change_log['from_sn']        =   $newsong_redbag['total_sn'];
                                    $update_change_log                  =   array();
                                    $update_change_log['status']        =   1;
                                    $update_change_log['update_time']   =   date('Y-m-d H:i:s',time());

                                    $Model->table('mj_lyricist_change_log')->where($where_change_log)->save($update_change_log);
                                    $Model->commit();
                                }
                                else
                                {
                                    $Model->rollback();
                                }
                            }
                            else
                            {
                                $Model->rollback();
                            }
                        }
                        else
                        {
                            $Model->rollback();
                        }

                    }
                }

                $data               =   $newsong_redbag['red_total'];
                $this->returnData($data,$msg='ok',$code=200);
            }
        }
        $this->returnData($data,$msg='参数错误',$code=401);
    }

    /**
     * 红包记录
     */
    public function redbag_log()
    {
        $data       =   array();
        $uid        =   I('post.uid','','htmlspecialchars');
        $appid = I('post.appid','','htmlspecialchars');
        if(! empty($uid))
        {
            $where          =   array();
            $where['uid']   =   $uid;
            $field          =   array('uid');
            $result         =   M('lyricist_member')->where($where)->field($field)->find();

            if (! empty($result))
            {
                $where_redbag_log               =   array();
                $field2                          =   array('id','challenge_id','game_id','member_id','red_total','status','create_time','update_time','total_sn','time');
                $where_redbag_log['member_id']  =   $uid;
                $where_redbag_log['status']     =   1;
                $data   =   M('lyricist_redbag_log')->where($where_redbag_log)->field($field2)->select();
            }
        }

        $this->returnData($data,$msg='ok',$code=200);
    }


    /**
     * 提现
     */
    public function get_cash()
    {
        $data       =   array();
        $uid        =   I('post.uid','','htmlspecialchars');
        $money      =   I('post.money','','htmlspecialchars');
        $appid = I('post.appid','','htmlspecialchars');
        if (! empty($uid) && ! empty($money) && $money > 0)
        {
            $where          =   array();
            $where['uid']   =   $uid;
            $field          =   array('uid','money_total','lock_money','nickname','avatarurl');
            $member_data    =   M('lyricist_member')->where($where)->field($field)->find();
            if (! empty($member_data))
            {
                //判断是否有体现申请
                $where_cash                 =   array();
                $field_cash                 =   array('id','order_id');
                $where_cash['uid']          =   $uid;
                $where_cash['cash_status']  =   0;
                $newsong_cash               =   M('lyricist_cash')->where($where_cash)->field($field_cash)->find();

                if (! empty($newsong_cash))
                {
                    $data['msg']        =   '你有一笔提现申请再审核中，不能继续提现';
                    $data['order_id']   =   $newsong_cash['order_id'];
                    $this->returnData($data,$msg='你有一笔提现申请再审核中，不能继续提现',$code=400);
                }
                else
                {
                    //判断金额限制
                    if ($member_data['money_total'] < $money)
                    {
                        $data['msg']    =   '非法操作金额';
                        $this->returnData($data,$msg='非法操作金额!',$code=400);
                    }

                    //5元以上才能提现
                    if ($member_data['money_total'] < 5)
                    {
                        $data['msg']    =   '金额要5元以上才能提现';
                        $this->returnData($data,$msg='Think You !',$code=400);
                    }

                    //插入数据
                    $insert_order                   =   array();
                    $insert_order['uid']            =   $uid;
                    $insert_order['nickname']       =   $member_data['nickname'];
                    $insert_order['avatarurl']      =   $member_data['avatarurl'];
                    $insert_order['order_id']       =   'TX'.date('YmdHis').rand(100000,999999);
                    $insert_order['money']          =   $money;
                    $insert_order['create_time']    =   time();
                    $insert_order['appid']    =   $appid;

                    //更新数据
                    $update_member                  =   array();
                    $update_member['lock_money']    =   $money;
                    $update_member['money_total']   =   $member_data['money_total'] -  $money;
                    $update_member['update_time']   =   time();

                    //开启事务
                    $Model = M();
                    $Model->startTrans();

                    $result_cash        =   $Model->table('mj_lyricist_cash')->add($insert_order);

                    $result_member      =   $Model->table('mj_lyricist_member')->where($where)->save($update_member);

                    if (! empty($result_cash) && ! empty($result_member))
                    {
                        $Model->commit();

                        //发送推送消息（用户的和客服的）

                        $data['msg']    =   '提现成功！等待审核！';
                        $data['kefu']   =   'onetechclub';
                        $data['order_id']   =   $insert_order['order_id'];
                        $this->returnData($data,$msg='Think you !',$code=200);
                    }
                    else
                    {
                        $Model->rollback();
                        $data['msg']    =   '提现失败';
                        $this->returnData($data,$msg='提现失败',$code=400);
                    }

                }

            }
        }

        $data['msg']    =   '非法操作';
        $this->returnData($data,$msg='非法操作',$code=400);
    }


    //提现记录
    public function cash_log()
    {
        $data       =   array();
        $uid        =   I('post.uid','','htmlspecialchars');
        $appid = I('post.appid','','htmlspecialchars');
        if(! empty($uid))
        {
            $where          =   array();
            $where['uid']   =   $uid;
            $field          =   array('uid');
            $result         =   M('lyricist_member')->where($where)->field($field)->find();

            if (! empty($result))
            {
                $where_cash_log                 =   array();
                $field                          =   array('id','uid','money','order_id','create_time','cash_status');
                $where_cash_log['uid']          =   $uid;
                $where_cash_log['cash_status']  =   array('EGT','0');
                $result   =   M('lyricist_cash')->where($where_cash_log)->field( $field )->select();
                if (! empty($result))
                {
                    foreach ($result as $key=> $val)
                    {
                        $val['create_time'] =   date('Y-m-d H:i:s',$val['create_time']);
                        $data[] =   $val;
                    }

                }
            }
        }

        $this->returnData($data,$msg='ok',$code=200);

    }


    /**
     * 存放模板消息
     */
    public function send()
    {
        $appid = I('post.appid','','htmlspecialchars');
        $touser = I('post.touser','','htmlspecialchars');
        $page = I('post.page','','htmlspecialchars');
        $form_id = I('post.form_id','','htmlspecialchars');
        $form_type = I('post.form_type','','htmlspecialchars');
        $secret = I('post.secret','','htmlspecialchars');
        $member_id = I('post.uid','','int');
        $template_id = I('post.template_id','1','htmlspecialchars');

        if ($touser==""||$form_id==""){
            $this->returnData('','参数错误','401');
        }

        $date = date('Y-m-d');
        $insert = array(
            'appid'=>$appid,
            'touser'=>$touser,
            'template_id'=>$template_id,
            'page'=>$page,
            'form_id'=>$form_id,
            'form_type'=>$form_type?$form_type:1,
            'status'=>'-1',
            'add_time'=>time(),
            'secret'=>$secret,
            'createtime'=>$date,
            'uid'=>$member_id,
        );

        M('lyricist_template_send')->data($insert)->add();
        $this->returnData('成功','200');



    }



}